package com.tjh.see.aspect;

import com.tjh.see.common.WebPageInfo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 拦截controller处理
 */
@Component
@Aspect
@Slf4j
public class ControllerAspect {
    private final static Logger logger = LoggerFactory.getLogger(ControllerAspect.class);

    @Pointcut("execution(* com.tjh.see.controller..*(..))")
    public void executeController(){}

    @Before("executeController()")
    public void before(){
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = requestAttributes.getRequest();
            WebPageInfo webPageInfo = (WebPageInfo) request.getAttribute("webPageInfo");
            if (webPageInfo == null) {
                logger.info("手动创建Logger对象：未设置TDK");
                log.info("注解方式使用日志：将使用默认TDK");
                //TDK为空时设置一个tdk
                webPageInfo = new WebPageInfo(
                        "T-JK",
                        "个人博客",
                        "blog"
                );
                request.setAttribute("webPageInfo", webPageInfo);
            }
        }
    }


}
